package com.hyg.lib_music.ui.view;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.hyg.lib_common.DataModel.Music.Music;
import com.hyg.lib_music.R;
import com.hyg.lib_music.presenter.di.scopes.Scopes;
import com.hyg.lib_music.service.MusicService;
import com.hyg.lib_music.ui.activity.MusicMainActivity;
import com.hyg.lib_music.ui.activity.MusicPlayingDetailActivity;
import com.hyg.lib_music.ui.activity.PlayingListActivity;
import com.hyg.lib_music.ui.activity.TestingResultActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: Administrator
 * @date: 2021/4/27
 */
public class NewMusicPlayer extends LinearLayout {

    ImageView musicIcon, menuIv, PlayBtn;
    TextView nameTv;
    LinearLayout linearLayout;
    Activity context;

    public NewMusicPlayer(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public void init(Context context) {
        this.context = (Activity) context;
        View view = LayoutInflater.from(context).inflate(R.layout.view_music_play_controller, this, true);
        musicIcon = view.findViewById(R.id.iv_music_image);
        nameTv = view.findViewById(R.id.tv_music_name);
        menuIv = view.findViewById(R.id.iv_music_menu);
        PlayBtn = view.findViewById(R.id.iv_music_play_control);
        linearLayout = view.findViewById(R.id.root_play_controller);
    }

    public void setMenuVisiable(int isVisiable){
        menuIv.setVisibility(isVisiable);
    }


    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        initPlayerView();
    }

    public void initPlayerView() {
        if (MusicMainActivity.serviceBinder != null && MusicMainActivity.serviceBinder.isPlaying() && MusicMainActivity.serviceBinder.getCurrentMusic() != null) {
            Music music = MusicMainActivity.serviceBinder.getCurrentMusic();
            linearLayout.setVisibility(View.VISIBLE);
            nameTv.setText(music.musicName);
            PlayBtn.setImageResource(R.mipmap.new_play_controler_pause);
            Glide.with(context).load(MusicMainActivity.serviceBinder.getImagePath()).into(musicIcon);
        }
        MusicMainActivity.serviceBinder.registerOnStateChangeListener(listenr);


        linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context,
                        MusicPlayingDetailActivity.class);
                context.startActivity(intent);
                context.overridePendingTransition(R.anim.in_from_down, R.anim.hold);
            }
        });

        PlayBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MusicMainActivity.serviceBinder.playOrPause();
            }
        });


        menuIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<Music> playList = MusicMainActivity.serviceBinder.getPlayingList();
                Intent intent = new Intent(context,
                        PlayingListActivity.class);
                intent.putParcelableArrayListExtra("data", (ArrayList<? extends Parcelable>) playList);
                context.startActivity(intent);
                context.overridePendingTransition(R.anim.in_from_down, R.anim.hold);
            }
        });
    }


    //实现监听器监听MusicService的变化，
    private MusicService.OnStateChangeListenr listenr = new MusicService.OnStateChangeListenr() {
        @Override
        public void onPlayProgressChange(long played, long duration) {

        }

        @Override
        public void onPlay(final Music item) {
            Log.i("lgb", "onPlay: ");
            if (linearLayout.getVisibility() != View.VISIBLE) {
                linearLayout.setVisibility(View.VISIBLE);
            }
            nameTv.setText(item.musicName);
            PlayBtn.setImageResource(R.mipmap.new_play_controler_pause);
            Glide.with(context).load(MusicMainActivity.serviceBinder.getImagePath()).into(musicIcon);
        }

        @Override
        public void onPause() {
            //变为暂停状态时
            PlayBtn.setImageResource(R.mipmap.new_music_play_btn);
        }
    };

    public void release() {
        MusicMainActivity.serviceBinder.unregisterOnStateChangeListener(listenr);
        listenr = null;
    }


    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        release();
    }
}
